<template>
	<view>
		<view class="infoItem">
			<view class="infoItem-left">
				<view class="infoItem-title">手机号：</view>
				<input type="number" v-model="setPhone" placeholder="请输入要绑定的新手机号" placeholder-class="inputDefault" />
			</view>
		</view>
		<view class="infoItem">
			<view class="infoItem-left">
				<view class="infoItem-title">验证码：</view>
				<input type="number" v-model="setCode" placeholder="请输入验证码" placeholder-class="inputDefault" />
			</view>
			<view class="code" :class="{'codeGet':isGet}" @click="getCode()">{{codeText}}</view>
		</view>
		<view class="saveBtn" @tap="changePhone">提交</view>
	</view>
</template>

<script>
	import Checker from '@/common/graceChecker.js';
	export default {
		data() {
			return {
				setPhone: '', //设置手机号
				setCode: '', //设置手机验证码
				isGet: false, //判断是否获取验证码
				codeText: '获取验证码', //手机号验证码
				count: 0, //获取验证码倒计时时间
				timer: null, //定时器
			}
		},
		methods: {
			// 设置手机号
			changePhone() {
				//验证规则
				var rule = [{
						name: 'phone',
						checkType: 'phoneno',
						checkRule: '',
						errorMsg: '请输入正确的手机号'
					},
					{
						name: "code",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "验证码不能为空"
					}
				];
				//验证数据
				var data = {
					phone: this.setPhone,
					code: this.setCode
				};
				var getCheck = Checker.check(data, rule);
				if (!getCheck) {
					this.util.showDialog(Checker.error);
					return;
				}
				var data = {
					phone: this.setPhone,
					access_code: this.setCode
				}
				this.$api.apiPost('user/currency.basic/bindUserPhone', data).then(res => {
					this.util.showDialog(res.data.msg);
					if (res.data.code == 200) {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							})
						}, 1000);
					}
				})
			},
			// 判断是否发送验证码
			getCode() {
				if (this.setPhone != "") {
					var yanData = {
						phone: this.setPhone,
						code_type: 'access_code'
					}
					this.$api.apiPost('user/currency.Code/SendCode', yanData).then(res => {
						if (res.data.code == 200) {
							this.getInterval(); //定时器
						} else {
							this.util.showDialog(res.data.msg);
						}
					})
				} else {
					this.util.showDialog('请输入手机号');
				}
			},
			// 获取验证码倒计时
			getInterval() {
				var that = this;
				uni.showToast({
					title: '短信发送成功',
					icon: 'none',
					duration: 1000,
					success: function(res) {
						const TIME_COUNT = 60;
						that.count = TIME_COUNT;
						that.timer = setInterval(() => {
							if (that.count > 0 && that.count <= TIME_COUNT) {
								// 倒计时时不可点击
								that.isGet = true;
								// 计时秒数
								that.count--;
								// 更新按钮的文字内容
								that.codeText = that.count + 's后重新获取';
							} else {
								// 倒计时完，可点击
								that.isGet = false;
								// 更新按钮文字内容
								that.codeText = '获取验证码';
								// 清空定时器!!!
								clearInterval(that.timer);
								that.timer = null;
							}
						}, 1000);
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.infoItem-left {
		width: 70%;
		display: inline-flex;
		align-items: center;

		.infoItem-title {
			font-weight: bold;
		}

		.head_image {
			width: 68upx;
			height: 68upx;
		}
	}

	.code {
		width: 30%;
		color: #E6B873;
		text-align: center;
		white-space: nowrap;
	}

	.codeGet {
		color: #CDCDCD;
	}
</style>
